<?php
 
ini_set('memory_limit','16M');

require_once( '/home/cyberpower678/Peachy/Init.php' );

$site = Peachy::newWiki( "cyberbot i" );

$site->set_runpage("User:Cyberbot I/Run/FFD");
 
$ffdroot = $site->initPage( 'Wikipedia:Files for deletion' );
 
$ffdpages = array_filter( $ffdroot->get_templates(), 'ffd' ); //array of all pages included in this page that appear to be daily subpages
 
preg_match('/==\s*Old discussions\s*==\s(.*?)\s*==/is',$ffdroot->get_text(),$match);
$holdingcell = $match[1];
 
preg_match_all('/\*\[\[\/(.*?)\]\]/',$holdingcell,$matches);
 
foreach ($matches[1] as $page) {
    $ffdpages[] = 'Wikipedia:Files for deletion/'. $page;
}
 
$ffdpages = array_values($ffdpages); //sequentially numeric-indexed array
 
 
for ($j = 0 ; $j < count($ffdpages) ; ++$j) {
    $title =& $ffdpages[$j];
 
    $page = initPage($title);
 
    $date = substr($title,32);
 
    $array = preg_split('/==== ?\[\[:?(File:.*?)(?:\||\]\])/',$page->get_text(),-1, PREG_SPLIT_DELIM_CAPTURE  );
    unset ($array[0]); //first index is the description at the top of the page
    $array = array_values($array);
    for ($i = 0 ; $i < count($array) ; $i = $i + 2) { //even indices are image names; odd indices are discussions
        if (strpos($array[$i + 1],'xfd-closed')) {
            $page = initPage($array[$i]);
 
            if ($page->get_exists()) { //page was kept
                    $j = 0;
                    
                    $image = initImage( $array[$i] );
                    $pages = $image->get_usage();
 
                    foreach ($pages as $title) {
                        kept($title,$array[$i]);
                    }
            }
        } else { //PUI still open
                $j = 0;
                $image = initImage( $array[$i] );
                $pages = $image->get_usage();
 
                foreach ($pages as $title) {
                    open($title,$array[$i],$date);
                }
        }
    }
}
 
function kept($title,$imagename) {
	global $site;
    $i = 0;
    
    $page = initPage($title);
    $text = $page->get_text();
 
    $regex = '/\[\[' . preg_replace ('/^File/i','(?:(?:File)|(?:Image))',preg_quote($imagename,'/')) . '.*/is';
 
    preg_match_all($regex,$page->get_text(),$matches);
 
    foreach ($matches[0] as $match) {
        $counter = 2;
        $position = 2;
 
        while ($position < strlen($match)) {
            if ($match[$position] == '[') {
                ++$counter;
            } elseif ($match[$position] == ']') {
                --$counter;
            }
 
            ++$position;
 
            if ($counter == 0) {
                break 1; //the while loop only
            }
        }
 
        $imageuse = substr($match,0,$position);
 
        if (false !== stripos($imageuse,'ffdc')) {
            $template = new Template ($imageuse,'ffdc');
        } elseif (false !== stripos($imageuse,'ifdc')) {
            $template = new Template ($imageuse,'ifdc');
        } else {
            return;
        }
 
        if (isset($template)) {
            $newimageuse = preg_replace('/[\s\|]*' . preg_quote($template->templatestring,'/') . '/','',$imageuse);
            $text = str_replace($imageuse,$newimageuse,$text);
        }
    }
 
    $page->edit($text,'[[Wikipedia:Bots/Requests for approval/Sambot 12|BOT]]: removing {{ffdc}} from non-deleted image');
}
 
function open ($title,$imagename,$puiname) {
    $i = 0;
    
    $page = initPage($title);
 
    $text = $page->get_text();
 
    $regex = '/\[\[' . preg_replace ('/^File/i','(?:(?:File)|(?:Image))',preg_quote($imagename,'/')) . '.*/is';
 
    preg_match_all($regex,$page->get_text(),$matches);
 
    foreach ($matches[0] as $match) {
        $counter = 2;
        $position = 2;
 
        while ($position < strlen($match)) {
            if ($match[$position] == '[') {
                ++$counter;
            } elseif ($match[$position] == ']') {
                --$counter;
            }
 
            ++$position;
 
            if ($counter == 0) {
                break 1; //the while loop only
            }
        }
 
        $imageuse = substr($match,0,$position);
 
        if (preg_match('/[\|]?\{\{\s*[if]fdc/i',$imageuse)) {
            continue;
        }
 
        $current = '';
        $template = 0;
        $link = false;
 
        for ($i = 2 ; $i < strlen($imageuse) ; $i++) {
            if ($current && !$template && !$link && ($imageuse[$i] == '|')) {
                $bits[] = $current;
                $current = '';
                continue;
            }
 
            if (!$template && !$link && ($imageuse[$i] == ']') && ($imageuse[$i + 1] == ']')) {
                if ($current) {
                    $bits[] = $current;
                } elseif ($imageuse[$i - 1] == '|') {
                    $bits[] = '';
                }
 
                break;
            }
 
            if (($imageuse[$i] == '{') && ($imageuse[$i + 1] == '{')) {
                $template++;
            }
 
            if (($imageuse[$i] == '}') && ($imageuse[$i + 1] == '}')) {
                $template--;
            }
 
            if (!$link && ($imageuse[$i] == '[') && ($imageuse[$i + 1] == '[')) {
                $link = true;
            }
 
            if ($link && ($imageuse[$i] == ']') && ($imageuse[$i + 1] == ']')) {
                $link = false;
            }
 
            $current .= $imageuse[$i];
        }
 
        $link = '[[' . $bits[0];
 
        array_shift($bits);
 
        $added = false;
        $addlink = '';
        $regex = '/^\s*(?:(?:thumb)|(?:frame)|(?:border)|(?:right)|(?:left)|(?:center)|(?:none)|(?:link=)|(?:\d+\s*px)|(?:\d+x\d+\s*px(?:px)?)\s*$)/i';
 
        for ($i = count($bits) - 1 ; $i >= 0 ; --$i) {
            $bit = $bits[$i];
 
            if (preg_match($regex,$bit)) {
                $addlink = '|' . $bit . $addlink;
            } else {
                if (!$added) {
                    $bit .= ' {{ffdc|' . preg_replace('/^\s*(?:(?:File)|(?:Image)):/i','',$imagename) . '|' . $puiname . '}}';
                    $added = true;
                }
                $addlink = '|' . $bit . $addlink;
            }
        }
 
        if (!$added) {
            $addlink .= '| {{ffdc|' . preg_replace('/^\s*(?:(?:File)|(?:Image)):/i','',$imagename) . '|' . $puiname . '}}';
        }
 
        $link .= $addlink . ']]';
 
        $text = str_replace($imageuse,$link,$text);
    }
 
    $page->edit($text,'[[Wikipedia:Bots/Requests for approval/Sambot 12|BOT]]: tagging [[:' . $imagename . ']] as {{[[Template:ffdc|ffdc]]}}');
}
 
function ffd($name) {
    if (preg_match('/^Wikipedia\:Files for deletion\/\d{4}/',$name)) {
        return true;
    } else {
        return false;
    }
}


